using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections;
using System.IO;

/// <summary>
/// Summary description for SearchProcess
/// </summary>
public class SearchProcess
{
    ProcessUser processUser;
	public SearchProcess()
	{
		
	}

    public DataTable addRating(DataTable tblExpert)
    {
        try
        {
            tblExpert.Columns.Add("Rating");
            tblExpert.Columns.Add("TotalComment");
            for (int i = 0; i < tblExpert.Rows.Count; i++)
            {
                processUser.selectComment(tblExpert.Rows[i]["UserID"].ToString());
                tblExpert.Rows[i]["Rating"] = ProcessUser.dataset.Tables["Comment_Expert"].Rows[0]["Rating"].ToString();
                tblExpert.Rows[i]["TotalComment"] = ProcessUser.dataset.Tables["Comment_Expert"].Rows[0]["TotalComment"].ToString();
            }
        }
        catch (Exception ex)
        {
            HttpContext.Current.Response.Redirect("UserThongbao.aspx?id=" + HttpUtility.UrlEncode(ex.ToString()));
        }
        return tblExpert;
    }
 
    public string search_expert_advance(string name, string des, string know, string exper, string p_chat_to, string p_chat_from, string p_mail_to, string p_mail_from, bool rating, string rate, bool online, string catID)
    {
        string datafile = "";
        try
        {
            processUser = new ProcessUser();
            processUser.selectExpert();
            
            DataView dvUser = addRating(ProcessUser.dataset.Tables["Expert"]).DefaultView;
            
            string filter = "UserName like '%" + name + "%' AND BriefDescription like '%" + des + "%'"
                               + " OR DetailDescription like '%" + des + "%' AND Knowledge like '%" + know + "%'"
                               + " AND Experience like '%" + exper + "%'";
            if (p_chat_from != "" && p_chat_to != "")
            {
                filter = filter + " AND ChatPrice < " + int.Parse(p_chat_to) + " AND ChatPrice > " + int.Parse(p_chat_from);
            }
            if (p_mail_from != "" && p_mail_to !="")
            {
                filter = filter + " AND EmailPrice < " + int.Parse(p_mail_from) + " AND EmailPrice > " + int.Parse(p_mail_from);
            }
            if (catID != "")
            {
                filter = filter + " AND CatID = " + int.Parse(catID);
            }
            if (online)
            {
                filter = filter + " AND Status = 1";
            }
            if (rate != "")
            {
                filter = filter + " AND Rating = " + int.Parse(rate);
            }
            if (rating)
            {
                dvUser.Sort = "Rating";
            }
            dvUser.RowFilter = filter;
            
            CreateXML_Expert xmlexpert = new CreateXML_Expert();
            datafile = xmlexpert.writeXML(dvUser);
            
        }
        catch(Exception ex)
        {
            HttpContext.Current.Response.Redirect("UserThongbao.aspx?id=" + HttpUtility.UrlEncode(ex.ToString()));
        }
        return datafile;
    }

    public string search_expert_category(string catID)
    {
        string datafile = ""; 
        try
        {
            processUser = new ProcessUser();
            processUser.selectExpert();
            DataView dvUser = addRating(ProcessUser.dataset.Tables["Expert"]).DefaultView;
            dvUser.RowFilter = "CatID=" + catID;
            CreateXML_Expert xmlExpert = new CreateXML_Expert();
            datafile = xmlExpert.writeXML(dvUser);
            
        }
        catch (Exception ex)
        {
            HttpContext.Current.Response.Redirect("UserThongbao.aspx?id=" + HttpUtility.UrlEncode(ex.ToString()));
        }
        return datafile;
    }

    public void search_expert_all()
    {
        try
        {


        }
        catch (Exception ex)
        {
            HttpContext.Current.Response.Redirect("UserThongbao.aspx?id=" + HttpUtility.UrlEncode(ex.ToString()));
        }
    }
}
